package day11.ProducerConsumer;

import java.util.Random;

public class Producer extends Thread{
    private Stack stack;
    public Producer(Stack stack) {
        this.stack = stack;
    }

    @Override
    public void run() {
        while(true){
            char c = (char)(new Random().nextInt(26) + 'a');
            synchronized (stack){
                while(stack.isFull()){
                    try {
                        // 在栈对象上等待
                        // 收到通知后，会醒过来继续执行
                        stack.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                stack.push(c);
                System.out.println("压入<--"+c);
                // 通知在栈对象上等待的线程
                stack.notifyAll();
            }
        }
    }
}
